package cn.org.mrliu.ssm.web.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.mrliu.ssm.common.web.controller.BaseController;
import cn.org.mrliu.ssm.common.web.vo.QueryParam;
import cn.org.mrliu.ssm.common.web.vo.ResponseMessage;
import cn.org.mrliu.ssm.constant.LogConstant;
import cn.org.mrliu.ssm.constant.RequestUrl;
import cn.org.mrliu.ssm.model.Customer;
import cn.org.mrliu.ssm.service.CustomerService;
import cn.org.mrliu.ssm.util.Pagination;

@Controller
@RequestMapping("/customerCache")
public class CustomerCacheController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(CustomerCacheController.class);
	private static final String DESCRIPTION = "Customer";
	@Resource
	private CustomerService customerService;

	/**
	 * 根据ID获取对象数据
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.DETAIL)
	public String detail(@PathVariable("id") Integer id, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "detail");
		log.debug(LogConstant.ID, id);
		Customer customer = customerService.getCustomer(id);
		model.addAttribute("customer", customer);
		log.debug(LogConstant.MODEL + model);
		return "customer/detail";
	}

	/**
	 * 保存数据
	 * 
	 * @param customer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.SAVE, method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage save(Customer customer, BindingResult result) throws Exception {
		if (result.hasErrors()) {
			System.out.println("错误...");
			ResponseMessage responseMessage = new ResponseMessage();
			responseMessage.setSuccess(false);
			responseMessage.setMessage(getValidatorErrorMessageList(result));
			responseMessage.setData(customer);
			return responseMessage;
		} else {
			System.out.println("正确");
		}
		log.debug(LogConstant.EXCUTE_METHOD, "save");
		log.debug("customer:" + customer);
		try {
			customerService.addCustomer(customer);
			log.info(LogConstant.SAVE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("保存成功!");
		} catch (Exception e) {
			log.error(LogConstant.SAVE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "保存失败!");
	}

	/**
	 * 跳转到编辑页面之前获取对象数据
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.EDIT)
	public String edit(@PathVariable("id") Integer id, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "edit");
		log.debug(LogConstant.ID, id);
		Customer customer = customerService.getCustomer(id);
		model.addAttribute("customer", customer);
		log.debug(LogConstant.MODEL + model);
		return "customer/edit";
	}

	/**
	 * 保存数据
	 * 
	 * @param customer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.UPDATE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage update(Customer customer) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "update");
		log.debug("customer:" + customer);
		try {
			customerService.updateCustomer(customer);
			log.info(LogConstant.UPDATE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.UPDATE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "修改失败!");
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.DELETE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delete(@PathVariable("id") Integer id) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "delete");
		log.debug(LogConstant.ID, id);
		try {
			customerService.deleteCustomer(id);
			log.info(LogConstant.DELETE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("删除成功!");
		} catch (Exception e) {
			log.info(LogConstant.DELETE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "删除失败!");
	}

	/**
	 * 获取分页列表
	 * 
	 * @param queryParam
	 *            查询参数
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.LIST)
	public String list(QueryParam queryParam, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "list");
		log.debug("queryParam:" + queryParam);
		Pagination pagination = customerService.getCustomerList(1, 10, "Hello");
		model.addAttribute("customerList", pagination);
		log.debug(LogConstant.MODEL + model);
		return "customer/list";
	}
}
